home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Mathematics / Notebooks / SigProc2.0 / Packages / SignalProcessing / ObjectOriented / StackQueue.m < prev    next >
Encoding:
Text File  |  1992-08-18  |  3.7 KB  |  163 lines

  1. (*  :Title:    Stacks and Queues  *)
  2.  
  3. (*  :Authors:    Brian Evans, James McClellan  *)
  4.  
  5. (*  :Summary:    To provide stack and queue structures  *)
  6.  
  7. (*  :Context:    SignalProcessing`ObjectOriented`StackQueue`  *)
  8.  
  9. (*  :PackageVersion:  2.7    *)
  10.  
  11. (*
  12.     :Copyright:    Copyright 1989-1991 by Brian L. Evans
  13.         Georgia Tech Research Corporation
  14.  
  15.     Permission to use, copy, modify, and distribute this software
  16.     and its documentation for any purpose and without fee is
  17.     hereby granted, provided that the above copyright notice
  18.     appear in all copies and that both that copyright notice and
  19.     this permission notice appear in supporting documentation,
  20.     and that the name of the Georgia Tech Research Corporation,
  21.     Georgia Tech, or Georgia Institute of Technology not be used
  22.     in advertising or publicity pertaining to distribution of the
  23.     software without specific, written prior permission.  Georgia
  24.     Tech makes no representations about the suitability of this
  25.     software for any purpose.  It is provided "as is" without
  26.     express or implied warranty.
  27.  *)
  28.  
  29. (*  :History:    *)
  30.  
  31. (*  :Keywords:    *)
  32.  
  33. (*  :Source:    *)
  34.  
  35. (*  :Warning:    *)
  36.  
  37. (*  :Mathematica Version:  1.2 or 2.0  *)
  38.  
  39. (*  :Limitation:  *)
  40.  
  41. (*  :Discussion:  *)
  42.  
  43. (*
  44.     :Functions:      ClearQueue
  45.           ClearStack
  46.           Dequeue
  47.           Enqueue
  48.           Pop
  49.           Push
  50.           TopElement
  51.  *)
  52.  
  53.  
  54.  
  55. (*  B E G I N     P A C K A G E  *)
  56.  
  57. BeginPackage[ "SignalProcessing`ObjectOriented`StackQueue`" ]
  58.  
  59.  
  60. If [ TrueQ[ $VersionNumber >= 2.0 ],
  61.      $NewMessage[ System`General, "spell" ];
  62.      $NewMessage[ System`General, "spell1" ];
  63.      Off[ General::spell ];
  64.      Off[ General::spell1 ] ];
  65.  
  66.  
  67. (*  U S A G E     I N F O R M A T I O N  *)
  68.  
  69. ClearQueue::usage =
  70.     "ClearQueue[q] sets q to be an empty queue."
  71.  
  72. ClearStack::usage =
  73.     "ClearStack[s] sets s to be an empty stack."
  74.  
  75. Dequeue::usage =
  76.     "Dequeue[queue, element] removes element from the queue."
  77.  
  78. Enqueue::usage =
  79.     "Enqueue[queue, element] adds element to the queue."
  80.  
  81. Pop::usage =
  82.     "Pop[stack] removes and returns the value at the top of the stack."
  83.  
  84. Push::usage =
  85.     "Push[stack, element] adds the element to the top of the stack."
  86.  
  87. Queue::usage =
  88.     "Object head for a queue structure."
  89.  
  90. TopElement::usage =
  91.     "TopElement[packet] returns the first element in the data packet. \
  92.     This is the top of the stack if packet is a stack. \
  93.     This is the next in the queue if packet is a queue. \
  94.     The packet remains unaltered."
  95.  
  96. (*  E N D     U S A G E     I N F O R M A T I O N  *)
  97.  
  98.  
  99. Begin[ "`Private`" ]
  100.  
  101.  
  102. (*  Q U E U E     R O U T I N E S  *)
  103.  
  104. SetAttributes[ClearQueue, {HoldFirst}]
  105. SetAttributes[Dequeue, {HoldFirst}]
  106. SetAttributes[Enqueue, {HoldFirst}]
  107.  
  108.  
  109. ClearQueue[queue_] := queue = Queue[];
  110.  
  111. Dequeue[queue_, element_] := Null    /; EmptyQ[queue]
  112. Dequeue[queue_, element_] :=
  113.     Block [    {index, pos},
  114.         pos = 0;
  115.         sametest[x_] := If [ SameQ[x, element], Return[pos], ++pos ];
  116.         Scan[sametest, queue];
  117.         If [ pos > 0,
  118.              queue = Drop[queue, {pos, pos}] ];
  119.         queue ]
  120.  
  121. Enqueue[queue_, element_] := AppendTo[queue, element]
  122.  
  123.  
  124. (*  S T A C K     R O U T I N E S  *)
  125.  
  126. SetAttributes[ClearStack, {HoldFirst}]
  127. SetAttributes[Pop, {HoldFirst}]
  128. SetAttributes[Push, {HoldFirst}]
  129.  
  130. ClearStack[stack_] := stack = Stack[];
  131.  
  132. Pop[stack_] := Null        /; EmptyQ[stack]
  133. Pop[stack_] :=
  134.     Block [ {newstack},
  135.         topelement = TopElement[stack];
  136.         stack = Rest[stack];
  137.         topelement ]
  138.  
  139. Push[stack_, element_] := PrependTo[stack, element]
  140.  
  141.  
  142. (*  G E N E R A L I Z E D     R O U T I N E S  *)
  143.  
  144. TopElement[x_] := Null        /; AtomQ[x]
  145. TopElement[h_[]] := Null
  146. TopElement[h_[values__]] := h[values][[1]]
  147.  
  148.  
  149. (*  E N D     P A C K A G E  *)
  150.  
  151. End[]
  152. EndPackage[]
  153.  
  154. If [ TrueQ[ $VersionNumber >= 2.0 ],
  155.      On[ General::spell ];
  156.      On[ General::spell1 ] ];
  157.  
  158.  
  159. (*  E N D I N G     M E S S A G E  *)
  160.  
  161. Print["Stack and queue structures successfully loaded."]
  162. Null
  163.